<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
	public $data;

	function __construct(){
		parent::__construct();
	}

	function login()
	{
		if($this->auth->checkLogin()) redirect('home/index');
		// Init View form
		$this->initView();
		// Check submit
		if($this->input->post('submit')){
			// Get email and password
			$email = trim($this->input->post('email'));
			$password = trim($this->input->post('password'));
			// Validate post data
			$this->load->library('form_validation');
			$this->form_validation->set_rules('email', 'Email', $this->validate->email());
			$this->form_validation->set_rules('password', 'Password', $this->validate->password());

			if ($this->form_validation->run())
			{
				// Get email and password from database
				$this->load->model('User_model');
				$user = $this->User_model->getUserInfo($email);
				// Check user exist
				if($user){
					// Check email and password
					if($email == $user[0]->email and MD5($password) == $user[0]->password){
						// Set session info user
						$this->setLogin($user[0]);
						redirect('home/index');
					}else{
						$this->data['message'] = "Email or password incorrect";
					}
				}else{// User not exist
					$this->data['message'] = "Email or password incorrect";
				}
			}
			// Set value form
			$this->data['email']['value'] = $email;
			$this->data['password']['value'] = $password;
		}
		// Render view
		$this->template->write_view('content', 'user/login', $this->data);
		$this->template->render();
	}

	function changepass(){
		if(!$this->auth->checkLogin()) redirect('home/index');
		// Check submit
		if($this->input->post('save')){
			// Validate post data
			$this->load->library('form_validation');
			$this->form_validation->set_rules('password', 'Password', $this->validate->password());
			$this->form_validation->set_rules('pass1', 'New password', $this->validate->password());
			$this->form_validation->set_rules('pass2', 'Confirm new Password', $this->validate->password()."|matches[pass1]");
			$this->form_validation->set_message('matches', 'Mật khẩu xác nhận không đúng');
			if ($this->form_validation->run()){
				// Get email and password from database
				$this->load->model('User_model');
				$user = $this->User_model->getUserInfo($this->session->userdata('Email'));
				$password = $this->input->post('password');
				$passconfirm = $this->input->post('pass1');
				// Check password
				$password = MD5($password);
				if($password == $user[0]->password){
					if($this->User_model->setPassword(MD5($passconfirm), $user[0]->id)){
						$this->data['message'] = "Thay đổi mật khẩu thành công";
					}
				}else{
					$this->data['message'] = "Mật khẩu không đúng";
				}
			}
		}
		if($this->input->post('cancel')){
			redirect('home/index');
		}
		// Render view
		$this->template->write_view('content', 'user/changepass', $this->data);
		$this->template->render();
	}

	function logout(){
		$this->session->sess_destroy();
		redirect('user/login');
	}

	function initView(){
		$this->data['email'] = array('name' => 'email', 'type' => 'text', 'class' => 'input-block-level','placeholder'=> 'Email address', 'maxlength' => '255');
		$this->data['password'] = array('name' => 'password', 'class' => 'input-block-level','placeholder'=> 'Password', 'maxlength' => '30', 'type' => 'password');
	}

	function setLogin($user){
		$data['User_id'] = $user->id;
		$data['Username'] = $user->uname;
		$data['Email'] = $user->email;
		$data['Group_id'] = $user->group_id;
		$data['Accept_flg'] = $user->accept_flg;

		$this->auth->setSessionUser($data);
	}
}